/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.fetcher.gui;

import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.NbPreferences;
import pl.tom.social.fetcher.common.FetcherConsts;

public final class FetcherSettingsPanel extends javax.swing.JPanel {

	private final FetcherSettingsOptionsPanelController controller;

	FetcherSettingsPanel(FetcherSettingsOptionsPanelController controller) {
		this.controller = controller;
		initComponents();
		// TODO listen to changes in form fields and call controller.changed()
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        crawlerPanel = new javax.swing.JPanel();
        maxIterLabel = new javax.swing.JLabel();
        maxIterTextField = new javax.swing.JTextField();
        containerPanel = new javax.swing.JPanel();
        mainHostLabel = new javax.swing.JLabel();
        mainHostTextField = new javax.swing.JTextField();
        mainPortLabel = new javax.swing.JLabel();
        mainPortTextField = new javax.swing.JTextField();
        agentPanel = new javax.swing.JPanel();
        maxAgentNrLabel = new javax.swing.JLabel();
        maxAgentNrTextField = new javax.swing.JTextField();

        crawlerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.crawlerPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(maxIterLabel, org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.maxIterLabel.text")); // NOI18N

        maxIterTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        maxIterTextField.setText(org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.maxIterTextField.text")); // NOI18N
        maxIterTextField.setMinimumSize(new java.awt.Dimension(100, 20));
        maxIterTextField.setPreferredSize(new java.awt.Dimension(100, 20));

        javax.swing.GroupLayout crawlerPanelLayout = new javax.swing.GroupLayout(crawlerPanel);
        crawlerPanel.setLayout(crawlerPanelLayout);
        crawlerPanelLayout.setHorizontalGroup(
            crawlerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, crawlerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(maxIterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 536, Short.MAX_VALUE)
                .addComponent(maxIterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        crawlerPanelLayout.setVerticalGroup(
            crawlerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(crawlerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(crawlerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxIterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maxIterLabel))
                .addContainerGap(157, Short.MAX_VALUE))
        );

        containerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.containerPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(mainHostLabel, org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.mainHostLabel.text")); // NOI18N

        mainHostTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        mainHostTextField.setText(org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.mainHostTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(mainPortLabel, org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.mainPortLabel.text")); // NOI18N

        mainPortTextField.setText(org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.mainPortTextField.text")); // NOI18N

        javax.swing.GroupLayout containerPanelLayout = new javax.swing.GroupLayout(containerPanel);
        containerPanel.setLayout(containerPanelLayout);
        containerPanelLayout.setHorizontalGroup(
            containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(containerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainHostLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mainHostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(mainPortLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mainPortTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        containerPanelLayout.setVerticalGroup(
            containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(containerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(containerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mainHostLabel)
                    .addComponent(mainHostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mainPortLabel)
                    .addComponent(mainPortTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        agentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.agentPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(maxAgentNrLabel, org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.maxAgentNrLabel.text")); // NOI18N

        maxAgentNrTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        maxAgentNrTextField.setText(org.openide.util.NbBundle.getMessage(FetcherSettingsPanel.class, "FetcherSettingsPanel.maxAgentNrTextField.text")); // NOI18N
        maxAgentNrTextField.setMinimumSize(new java.awt.Dimension(100, 20));
        maxAgentNrTextField.setPreferredSize(new java.awt.Dimension(100, 20));

        javax.swing.GroupLayout agentPanelLayout = new javax.swing.GroupLayout(agentPanel);
        agentPanel.setLayout(agentPanelLayout);
        agentPanelLayout.setHorizontalGroup(
            agentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(agentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(maxAgentNrLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 480, Short.MAX_VALUE)
                .addComponent(maxAgentNrTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        agentPanelLayout.setVerticalGroup(
            agentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(agentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(agentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxAgentNrLabel)
                    .addComponent(maxAgentNrTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(78, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(crawlerPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(containerPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(agentPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(containerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(agentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(crawlerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	void load() {
		mainHostTextField.setText(NbPreferences.forModule(FetcherSettingsPanel.class).get("mainHost", "127.0.0.1"));
		mainPortTextField.setText(NbPreferences.forModule(FetcherSettingsPanel.class).get("mainPort", "8888"));
		maxIterTextField.setText(String.valueOf(NbPreferences.forModule(FetcherSettingsPanel.class).getInt(
			"crawlerMaxIter", FetcherConsts.DEFAULT_MAX_ITERATIONS)));
		maxAgentNrTextField.setText(String.valueOf(NbPreferences.forModule(FetcherSettingsPanel.class).getInt(
			"crawlerMaxFetchers", FetcherConsts.DEFAULT_MAX_FETCHERS)));
	}

	void store() {
		NbPreferences.forModule(FetcherSettingsPanel.class).put("mainHost", mainHostTextField.getText());
		NbPreferences.forModule(FetcherSettingsPanel.class).put("mainPort", mainPortTextField.getText());
		NbPreferences.forModule(FetcherSettingsPanel.class).putInt("crawlerMaxIter", Integer.parseInt(maxIterTextField.
			getText()));
		NbPreferences.forModule(FetcherSettingsPanel.class).putInt("crawlerMaxFetchers", Integer.parseInt(
			maxAgentNrTextField.getText()));
	}

	boolean valid() {
		try {
			if(Integer.parseInt(maxIterTextField.getText()) < 0) {
				NotifyDescriptor nf = new NotifyDescriptor.Message("Nr of iterations must be > 0",
					NotifyDescriptor.INFORMATION_MESSAGE);
				DialogDisplayer.getDefault().notify(nf);
				return false;
			}
		} catch(NumberFormatException ex) {
			NotifyDescriptor nf = new NotifyDescriptor.Message("Max nr of iterations = " + Integer.MAX_VALUE,
				NotifyDescriptor.INFORMATION_MESSAGE);
			DialogDisplayer.getDefault().notify(nf);
			return false;
		}

		try {
			if(Integer.parseInt(maxAgentNrTextField.getText()) < 0) {
				NotifyDescriptor nf = new NotifyDescriptor.Message("Nr of agents must be > 0",
					NotifyDescriptor.INFORMATION_MESSAGE);
				DialogDisplayer.getDefault().notify(nf);
				return false;
			}
		} catch(NumberFormatException ex) {
			NotifyDescriptor nf = new NotifyDescriptor.Message("Max nr of iterations = " + Integer.MAX_VALUE,
				NotifyDescriptor.INFORMATION_MESSAGE);
			DialogDisplayer.getDefault().notify(nf);
			return false;
		}

		return true;
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel agentPanel;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JPanel crawlerPanel;
    private javax.swing.JLabel mainHostLabel;
    private javax.swing.JTextField mainHostTextField;
    private javax.swing.JLabel mainPortLabel;
    private javax.swing.JTextField mainPortTextField;
    private javax.swing.JLabel maxAgentNrLabel;
    private javax.swing.JTextField maxAgentNrTextField;
    private javax.swing.JLabel maxIterLabel;
    private javax.swing.JTextField maxIterTextField;
    // End of variables declaration//GEN-END:variables
}
